<?php

/**
 * uniBoard ACP Edit Group
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_group{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	/**
	 * Avaiable Ranks
	 *
	 * @var array
	 */
	
	private $ranks			= array();
	
	/**
	 * Avaiable Masks
	 *
	 * @var array
	 */
	
	private $masks			= array();
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Create API
		$this -> api = new api_user_group( $appCore, $appCore -> _REQ('group'));
		
		// Group Not Found
		if ( !$this -> api -> data_loaded)
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['acp_group_not_found'], $appCore -> lang['acp_user_groups']);
			
			// Jump back to Groups List
			new action_groups( $appCore);
		}
		else if ( $appCore -> _REQ( 'do') == 'edit')
		{
			// Get Masks
			$this -> masks = $appCore -> getMasks();
				
			// Add empty Rank
			$this -> ranks[0] = $appCore -> lang['rank_use_none'];
			
			// Ranks
			$appCore -> db -> query( 'SELECT rank_id, rank_name FROM ' . DB_PREFIX . 'user_ranks WHERE rank_special = \'1\'');
			
			// Pass them to array
			while ( $rank_result = $appCore -> db -> fetch_array())
			{
				// Pass to array
				$this -> ranks[$rank_result[0]] = ($appCore -> conf['board_int_ranks'] ? $appCore -> ifString( htmlspecialchars( $rank_result[1])) : htmlspecialchars( $rank_result[1]));
			}
			
			// Flush Result
			$appCore -> db -> freeResult();
			
			// Update?
			if ( $appCore -> _POST( 'update') == 1)
			{
				// Fill Api
				$this -> api -> setGroupName( $appCore -> _POST( 'group_name'));
				$this -> api -> setGroupPrefix( $appCore -> _POST( 'group_prefix'));
				$this -> api -> setGroupSuffix( $appCore -> _POST( 'group_suffix'));
				$this -> api -> setGroupRank( $appCore -> _POST( 'group_rank'));
				$this -> api -> setGroupHidden( $appCore -> _POST( 'group_hidden'));
				$this -> api -> setGroupMasks( $appCore -> listInput( 'group_masks', false, array_keys( $this -> masks)));
				
				// Api All-rite?
				if ( $this -> api -> error)
				{
					// Draw Errors
					$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> ifString( htmlspecialchars( $this -> api -> group_name)));
					
					// Repeat Form
					$this -> drawForm();
				}
				else
				{
					// Save Api
					$this -> api -> saveData();
					
					// Set Log
					$appCore -> logAdmin( 'log_admin_edited_user_group', htmlspecialchars( $appCore -> _POST( 'group_name')));
					
					// Redirect us back to list
					$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=groups'), $appCore -> lang['acp_group_edited']);
				}
			}
			else
			{
				// Draw Form
				$this -> drawForm();
			}
		}
		else if ( $appCore -> _REQ('do') == 'delete')
		{
			// System Group?
			if ( $this -> api -> group_system)
			{
				// Draw Error
				$appCore -> output -> error( $appCore -> lang['acp_group_is_system'], $appCore -> lang['acp_user_groups']);
				
				// Jump back to Groups List
				new action_groups( $appCore);
			}
			else if ( $appCore -> db -> count( 'users', 'group_id = \'' . $this -> api -> group_id . '\'') > 0)
			{
				// Draw Error
				$appCore -> output -> error( $appCore -> lang['acp_group_has_users'], $appCore -> lang['acp_user_groups']);
				
				// Jump back to Groups List
				new action_groups( $appCore);
			}
			else
			{
				// Kill Api
				$this -> api -> deleteData();
				
				// Set Log
				$appCore -> logAdmin( 'log_admin_deleted_user_group', htmlspecialchars( $this -> api -> group_name));
				
				// Draw Message
				$appCore -> output -> message( $appCore -> lang['acp_group_deleted'], $appCore -> ifString( htmlspecialchars( $this -> api -> group_name)));
			
				// Jump back to Groups List
				new action_groups( $appCore);
			}
		}
		else
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['wrong_action_code'], $appCore -> lang['acp_user_groups']);
			
			// Jump back to Groups List
			new action_groups( $appCore);
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_group&do=edit&group=' . $this -> api -> group_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_group_form_sect_name']);
		
		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_name'], $this-> appCore -> drawTextInput( 'group_name', $this -> api -> group_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_prefix'], $this-> appCore -> drawTextInput( 'group_prefix', $this -> api -> group_prefix), $this -> appCore -> lang['acp_group_form_affix_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_suffix'], $this-> appCore -> drawTextInput( 'group_suffix', $this -> api -> group_suffix), $this -> appCore -> lang['acp_group_form_affix_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_rank'], $this-> appCore -> drawList( 'group_rank', $this -> ranks, $this -> api -> group_rank));
		
		// New block
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_group_form_sect_display']);
		
		// Draw Yes No Field
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_hidden'], $this-> appCore -> drawYesNo( 'group_hidden', $this -> api -> group_hidden));
		
		// New block
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_group_form_sect_masks']);
		
		// Draw Perms Multilist
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_group_form_masks'], $this-> appCore -> drawMultiList( 'group_masks', $this -> masks, $this -> api -> group_masks));
				
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['edit_group_form'], array(
			$this -> appCore -> adminLink( 'act=groups') => $this -> appCore -> lang['acp_user_groups'],
			'' => $this -> appCore -> ifString( htmlspecialchars( $this -> api -> group_name))
		), ob_get_clean());
			
	}

}